package com.xiaobug.baselibrary.base;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ljb.commlibrary.utils.AutoUtils;
import com.ljb.commlibrary.utils.L;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by ljb on 2018/5/4.
 */

public abstract class BaseFragment<T extends ViewDataBinding> extends SupportFragment {
    public final String TAG = this.getClass().getSimpleName();

    /**
     * DataBinding
     **/
    protected T mBinding;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //判断该Activity设置设置了布局文件
        if (getRootID() == 0) {
            L.e(TAG, "getRootID = 0," + getClass().getSimpleName() + "将不显示界面!!!");
            return super.onCreateView(inflater, container, savedInstanceState);
        }
        //初始化DataBinding
        mBinding = DataBindingUtil.inflate(inflater, getRootID(), container, false);
        //初始化UI
        initUI(savedInstanceState, mBinding);
        //绑定数据
        initData(savedInstanceState, mBinding);
        //屏幕适配
        AutoUtils.auto(mBinding.getRoot());
        //返回根布局
        return mBinding.getRoot();
    }
    //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@一下方法为子类实现的方法@@@@@@@@@@@@@@@@@@@@@@@@@@@

    /**
     * 获取根布局ID
     * 必须重写该方法,否则界面不会显示
     *
     * @return
     */
    protected abstract int getRootID();

    /**
     * 初始化控件
     *
     * @param savedInstanceState
     * @param binding
     */
    protected void initUI(Bundle savedInstanceState, T binding) {
    }

    /**
     * 绑定数据
     *
     * @param savedInstanceState
     * @param binding
     */
    protected void initData(Bundle savedInstanceState, T binding) {
    }

}
